package com.yonyou.pmclouds.measuredata.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.measuredata.entity.MeasureDataVO;
import com.yonyou.pmclouds.measuredata.mapper.MeasureDataMapper;
import com.yonyou.pmclouds.measuredata.service.rmiitf.MeasureDataService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass=MeasureDataService.class)
public class MeasureDataServiceImpl implements MeasureDataService{
    @Autowired
    private MeasureDataMapper mapper;
    @Override
    public MeasureDataVO[] add(Double[] datas, String pk_bill) throws BusinessException {
        if(ArrayUtils.isEmpty(datas)) {
            throw new BusinessException("传入的要存入的测量数据为空!");
        }
        List<MeasureDataVO> dataVOList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            // 没有真实填值的项不进行保存
            if (datas[i] == null) {
                continue;
            }
            MeasureDataVO measureDataVO = new MeasureDataVO();
            measureDataVO.setPk_bill(pk_bill);
            measureDataVO.setNumber(dealNumber(datas[i]));
            measureDataVO.setSequence(i);
            dataVOList.add(measureDataVO);
        }
        if (dataVOList.size() > 0) {
            MultiInsertBpTemplate<MeasureDataVO> bp = new MultiInsertBpTemplate<>(mapper);
            bp.insert(dataVOList.toArray(new MeasureDataVO[dataVOList.size()]));
        }
        return dataVOList.toArray(new MeasureDataVO[dataVOList.size()]);
    }

    @Override
    public MeasureDataVO[] update(Double[] datas, String pk_bill) throws BusinessException {
        //删除现有测量数据 重新插入新数据
        mapper.deleteMeasureData(pk_bill);
        if(!ArrayUtils.isEmpty(datas)) {
            return add(datas, pk_bill);
        }
        return new MeasureDataVO[0];
    }

    @Override
    public MeasureDataVO[] queryByPkbill(String pk_bill) throws BusinessException {
        MeasureDataVO[] dataVOs = mapper.selectByPkbill(pk_bill);
        return dataVOs;
    }

    @Override
    public Map<String, List<MeasureDataVO>> getPkBillsMap(String[] pkBills) throws BusinessException {
        if (pkBills == null || pkBills.length == 0) {
            return new HashMap<>();
        }
        Map<String, List<MeasureDataVO>> pkMeasureMap = new HashMap<>();
        // 获取符合要求的全部数据
        MeasureDataVO[] measureDataVOS = mapper.selectByPKbills(pkBills);
        for (MeasureDataVO measureDataVO : measureDataVOS) {
            if (pkMeasureMap.get(measureDataVO.getPk_bill()) == null) {
                List<MeasureDataVO> measureList = new ArrayList<>();
                measureList.add(measureDataVO);
                pkMeasureMap.put(measureDataVO.getPk_bill(), measureList);
            } else {
                pkMeasureMap.get(measureDataVO.getPk_bill()).add(measureDataVO);
            }
        }
        return pkMeasureMap;
    }

    private double dealNumber(double number) throws BusinessException {
        if(number==0) {
            return 0;
        }
        String str = Double.toString(number);
        String[] split = str.split("\\.");
        if(split[0].length() > 10) {
            throw new BusinessException("测量数据输入过长");
        }
        // 四舍五入
        BigDecimal data = new BigDecimal(number);
        return data.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
